<include file="Public:header" />
<script type="text/javascript" charset="UTF-8" src="__PUBLIC__/Operation/js/my97date/wdatepicker.js"></script>
<script type="text/javascript" src="__PUBLIC__/Operation/js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="__PUBLIC__/Operation/js/swfupload/handlers.js"></script>	
<div class="headbar">
    <div class="position"><span>促销</span><span>></span><span>促销活动</span><span>></span><span><if condition="$gameRow.id neq null">编辑<else/>添加</if>{$name}</span></div>
    <ul class="tab" name="menu1">
        <li id="li_1" class="selected"><a href="javascript:void(0)" hidefocus="true" onclick="select_tab('1')">活动信息</a></li>
        <li id="li_2"><a href="javascript:void(0)" hidefocus="true" onclick="select_tab('2')">奖项设置</a></li>
    </ul>
</div>
<div class="content_box">
    <div class="content form_content">
        <form action="{:U('Game/game_edit_act')}"  method="post" name='game_edit' enctype='multipart/form-data'>
            <input type='hidden' name='id' value="{$gameRow['id']}"/>
            <input type='hidden' name='type_id' value="{$type_id}"/>
            <div id="table_box_1">
                <table class="form_table">
                    <col width="150px" />
                    <col />
                    <tr>
                        <th>活动名称：</th>
                        <td><input type='text' class='normal' name='title' pattern='required' alt='请填写活动名称' value="{$gameRow.title}"/><label>* 填写活动名称</label></td>
                    </tr>
                    <tr>
                        <th>活动时间：</th>
                        <td>
                            <input type='text' name='start_time' class='Wdate' pattern='datetime' onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" alt='请填写一个日期' value="{$gameRow['start_time']}"/> ～
                            <input type='text' name='end_time' class='Wdate' pattern='datetime' onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" alt='请填写一个日期' value="{$gameRow['end_time']}"/>
                            <label>* 此活动的时间段</label>
                        </td>
                    </tr>
                    <tr>
                        <th>抽奖次数：</th>
                        <td>
                            <input type='text' class='small' name='num' value="{$gameRow['num']}"/>
                        </td>
                    </tr>
                    <tr>
                        <th>是否开启：</th>
                        <td>
                            <label class='attr'><input type='radio' name='is_close' value='0' checked=checked />是</label>
                            <label class='attr'><input type='radio' name='is_close' value='1' />否</label>
                        </td>
                    </tr>
                    <tr>
                        <th>设置活动商品：</th>
                        <td>
                            <table class='border_table'>
                                <col width="100px" />
                                <col />
                                
                                <tbody id='regiment_box'>
                                </tbody>

                                <tr><td colspan=2><input type='hidden' name='goods_id' /><button type='button' class='btn' onclick='searchGoods("{:U('Block/search_goods', array('type' => radio))}",searchGoodsCallback);'><span>添加商品</span></button><label>* 添加活动商品</label></td></tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <th>排序：</th>
                        <td>
                            <input type='text' class='small' name='sort' value="{$gameRow['sort']}"/>
                        </td>
                    </tr>
                    <tr>
                        <th>活动规则：</th>
                        <td>
                            <textarea class='textarea' name='intro'>{$gameRow['intro']}</textarea>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="table_box_2" cellpadding="0" cellspacing="0" style="display:none">
                <table class="form_table">
                    <colgroup>
                        <col width="150px" />
                        <col />
                    </colgroup>
                    <tr>
                        <th>活动图片：</th>
                        <td>
                            <input class="middle" type="file" name="Filedata" />
                            <label>活动图片，格式为PNG</label>
                        </td>
                    </tr>
                    <tr>
                        <th>奖项：</th>
                        <td>
                            <table class='border_table' cellpadding="0" cellspacing="0" width='60%'>
                                <thead>
                                    <tr>
                                        <th>等级</th>
                                        <th>奖品</th>
                                        <th>概率</th>
                                        <th>数量</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody id='prize_box'>
                                <if condition="$prizeRow">
                                    <foreach name='prizeRow' item='item' key='key'>
                                        <tr class="td_c">
                                            <td><input type="hidden" name="prize[{$key}][id]" value="{$item.id}"/><input class="small" type="text" name="prize[{$key}][name]" value="{$item.prize_grade}" pattern="required" alt="填写等级" /></td>
                                            <td style="text-align: left">
                                                <select class="auto" name="prize[{$key}][type]" onchange="typeChange({$key})">
                                                    <option value="1" <if condition="$item.type eq 1">selected="selected"</if> >优惠券</option>
                                                    <option value="2" <if condition="$item.type eq 2">selected="selected"</if> >微信红包</option>
                                                    <option value="0" <if condition="$item.type eq 0">selected="selected"</if> >其他</option>
                                                </select>
                                                <label class="type{$key}">
                                                    <if condition="$item.type eq 1">
                                                    <select class="auto" name="prize[{$key}][content]">
                                                        <option value="">选择优惠券</option>
                                                        <foreach name='coupon' item='i' key='k'>
                                                        <option value="{$i.id}" <?php if($i['id'] == $item['prize_name']) echo ' selected="selected"'; ?> >{$i.title}</option>
                                                        </foreach>
                                                    </select>
                                                    <elseif condition="$item.type eq 2" />
                                                    <input class="small" type="text" name="prize[{$key}][content]" value="{$item.prize_name}" pattern="required" alt="填写红包金额" />元
                                                    <else />
                                                    <input class="normal" type="text" name="prize[{$key}][content]" value="{$item.prize_name}" pattern="required" alt="填写奖项" />
                                                    </if>
                                                </label>
                                            </td>
                                            <td><input class="small" type="text" name="prize[{$key}][chance]" value="{$item.chance}" /></td>
                                            <td><input class="small" type="text" name="prize[{$key}][number]" value="{$item.num}" /></td>
                                            <td></td>
                                        </tr>
                                    </foreach>
                                    <else/>
                                    <tr class="td_c">
                                        <td><input class="small" type="text" name="prize[0][name]" value="一等奖" pattern="required" alt="填写等级" /></td>
                                        <td style="text-align: left">
                                            <select class="auto" name="prize[0][type]" onchange="typeChange(0)">
                                                <option value="">奖品类型</option>
                                                <option value="1">优惠券</option>
                                                <option value="2">微信红包</option>
                                                <option value="0">其他</option>
                                            </select>
                                            <label class="type0"></label>
                                        </td>
                                        <td><input class="small" type="text" name="prize[0][chance]" value="" /></td>
                                        <td><input class="small" type="text" name="prize[0][number]" value="" /></td>
                                        <td></td>
                                    </tr>
                                    <tr class="td_c">
                                        <td><input class="small" type="text" name="prize[1][name]" value="二等奖" pattern="required" alt="填写等级" /></td>
                                        <td style="text-align: left">
                                            <select class="auto" name="prize[1][type]" id="type" onchange="typeChange(1)">
                                                <option value="">奖品类型</option>
                                                <option value="1">优惠券</option>
                                                <option value="2">微信红包</option>
                                                <option value="0">其他</option>
                                            </select>
                                            <label class="type1"></label>
                                        </td>
                                        <td><input class="small" type="text" name="prize[1][chance]" value="" /></td>
                                        <td><input class="small" type="text" name="prize[1][number]" value="" /></td>
                                        <td></td>
                                    </tr>
                                    <tr class="td_c">
                                        <td><input class="small" type="text" name="prize[2][name]" value="三等奖" pattern="required" alt="填写等级" /></td>
                                        <td style="text-align: left">
                                            <select class="auto" name="prize[2][type]" id="type" onchange="typeChange(2)">
                                                <option value="">奖品类型</option>
                                                <option value="1">优惠券</option>
                                                <option value="2">微信红包</option>
                                                <option value="0">其他</option>
                                            </select>
                                            <label class="type2"></label>
                                        </td>
                                        <td><input class="small" type="text" name="prize[2][chance]" value="" /></td>
                                        <td><input class="small" type="text" name="prize[2][number]" value="" /></td>
                                        <td></td>
                                    </tr>
                                </if>
                                    
                            </tbody>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><button type="button" class="btn" id="prizeAddButton"><span class="add">添 加</span></button></td>
                    </tr>
                    <tr>
                        <th>未中奖说明：</th>
                        <td>
                            <textarea class='textarea' pattern="required" name='failtext'>{$gameRow['failtext']}</textarea><label>* 未中奖提示</label>
                        </td>
                    </tr>
                </table>
            </div>
            <table class="form_table">
                <col width="150px" />
                <col />
                <tr>
                    <td></td>
                    <td><button class="submit" type="submit" ><span>确定</span></button></td>
                </tr>
            </table>
        </form>
    </div>
</div>

<script type='text/javascript'>
    //输入筛选商品的条件
    function searchGoodsCallback(goodsList)
    {
        goodsList.each(function()
        {
            var temp = $.parseJSON($(this).attr('data'));
            var content = {
                "data":
                    {
                    "id":temp.goods_id,
                    "name":temp.name,
                    "img":temp.img,
                    "sell_price":temp.sell_price,
                    "img":temp.img,
                    "start_id":temp.min,
                    "end_id":temp.max
                }
            };
            relationCallBack(content);
        });
    }

    //关联商品回调处理函数
    function relationCallBack(content,regimentImg)
    {
        if(content)
        {
            $('[name="goods_id"]').val(content['data']['id']);
            regimentImg = !regimentImg ? content['data']['img'] : regimentImg;

            var imgUrl = "__ROOT__/@url@";
            imgUrl     = imgUrl.replace("@url@",regimentImg);

            var html = '<tr><th>商品名称：</th><td>'+content['data']['name']+'</td></tr>'
                +'<tr><th>展示图片：</th><td><img src="'+imgUrl+'" title="'+content['data']['name']+'" style="max-width:140px;" /><br /></td></tr>'
                +'<tr><th>二维码开始ID：</th><td><input type="text" class="small" name="start_id" pattern="^[1-9]\\d*$" alt="填写数字" value="'+content['data']['start_id']+'"/>，<label>该商品下从这个ID开始参与活动的二维码ID</label></td></tr>'
                +'<tr><th>二维码结束ID：</th><td><input type="text" class="small" name="end_id" pattern="^[1-9]\\d*$" alt="填写数字" value="'+content['data']['end_id']+'" />，<label>该商品下到这个ID结束参与活动的二维码ID</label></td></tr>';

            $('#regiment_box').html(html);
        }
    }
    
    //tab标签切换
    function select_tab(curr_tab)
    {
        $("form[name='game_edit'] > div").hide();
        $("#table_box_"+curr_tab).show();
        $("ul[name=menu1] > li").removeClass('selected');
        $('#li_'+curr_tab).addClass('selected');
    }

    //关联商品信息
    var goodsRow ={$goodsRow};
    if(goodsRow){
        relationCallBack(goodsRow,"{$gameRow['img']}");
    }

    function getValHtml(i, name,content,chance,number)
    {
        if(name == undefined)
            name = '';
        if(content == undefined)
            content = '';
        if(chance == undefined)
            chance = '';
        if(number == undefined)
            number = '';
        if(i == undefined)
            i = 3;
        return '<td><input class="small" type="text" name="prize['+i+'][name]" value="'+name+'" pattern="required" alt="填写等级" /></td>'
            +'<td style="text-align: left">'
                +'<select class="auto" name="prize['+i+'][type]" onchange="typeChange('+i+')">'
                    +'<option value="">奖品类型</option>'
                    +'<option value="1">优惠券</option>'
                    +'<option value="2">微信红包</option>'
                    +'<option value="0">其他</option>'
                +'</select>'
                +'<label class="type'+i+'"></label>'
            +'</td>'
            +'<td><input class="small" type="text" name="prize['+i+'][chance]" value="'+chance+'" /></td>'
            +'<td><input class="small" type="text" name="prize['+i+'][number]" value="'+number+'" /></td>';
    }

    //根据显示类型返回格式
    function getTr(i)
    {
        var prizeInputHtml = getValHtml(i);

        //数据
        var prizeRow = '<tr class="td_c">'+prizeInputHtml+''
            +'<td><img class="operator" src="__PUBLIC__/Partner/images/admin/icon_asc.gif" alt="向上" />'
            +'<img class="operator" src="__PUBLIC__/Partner/images/admin/icon_desc.gif" alt="向下" />'
            +'<img class="operator" src="__PUBLIC__/Partner/images/admin/icon_del.gif" alt="删除" />'
            +'</td></tr>';

        return prizeRow;
    }
    //添加规格按钮(点击绑定)
    $('#prizeAddButton').click(
        function()
        {
            var prizeSize = $('#prize_box tr').size();
            //alert(prizeSize);die;
            var prizeRow = getTr(prizeSize);
            $('#prize_box').append(prizeRow);
            initButton(prizeSize);
        }
    )

    //按钮(点击绑定)
    function initButton(indexValue)
    {
        //功能操作按钮
        $('#prize_box tr:eq('+indexValue+') .operator').each(
            function(i)
            {
                switch(i)
                {
                    //向上排序
                    case 0:
                        $(this).click(
                        function()
                        {
                            var insertIndex = $(this).parent().parent().prev().index();
                            if(insertIndex >= 0)
                            {
                                $('#prize_box tr:eq('+insertIndex+')').before($(this).parent().parent());
                            }
                        }
                    )
                        break;

                    //向下排序
                    case 1:
                        $(this).click(
                        function()
                        {
                            var insertIndex = $(this).parent().parent().next().index();
                            $('#prize_box tr:eq('+insertIndex+')').after($(this).parent().parent());
                        }
                    )
                        break;

                    //删除排序
                    case 2:
                        $(this).click(
                        function()
                        {
                            var obj = $(this);
                            art.dialog.confirm('确定要删除么？',function(){obj.parent().parent().remove()});
                        }
                    )
                        break;
                }
            }
        )
    }


    function typeChange(i){
        var type = $('select[name="prize['+i+'][type]"]').find('option:selected').attr('value');
        //alert(type);die;
        if(type){
            var typeRow = getType(type, i);
            $('.type'+i).html('');
            $('.type'+i).append(typeRow);
        }else{
            $('.type'+i).hide();
        }
    }

    //根据显示类型返回格式
    function getType(type, i)
    {
        if(type == 1){
            var option = '{$option}';
            var typeRow = '<select class="auto" name="prize['+i+'][content]"><option value="">选择优惠券</option>'+option+'</select>';
        }else if(type == 2){
            var typeRow = '<input class="small" type="text" name="prize['+i+'][content]" value="" pattern="required" alt="填写红包金额" />元';
        }else{
            var typeRow = '<input class="normal" type="text" name="prize['+i+'][content]" value="" pattern="required" alt="填写奖项" />';
        }
        return typeRow;
    }
</script>
<include file="Public:footer" />